/**
 * Created by dylan_Apple on 17/6/25.
 */
var async = require('async');
var moment = require('moment');
var mConference = require('../controller/controller.mconference');
var user = require('../controller/controller.user');
var seller = require('../controller/controller.seller');
var comment = require('../controller/controller.comment');
var stockRecommend = require('../controller/controller.stockrecommend');
var industry = require('../controller/controller.industry');

module.exports = {

    //获取晨会信息
    getMConferenceInfo: function (req, res, next) {
        var params = req.query;
        async.series({
            mConference: function (callback) {
                mConference.getInfo(params, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            },
            commentList: function (callback) {
                params.type = 'morning';
                comment.getListByMconference(params, function (err, result) {
                    if (err) {
                        console.error("commentList error:", err);
                        res.redirect('/error');
                        return;
                    } else {
                        callback(null, result);
                    }
                })
            }
        }, function (err, results) {
            if (err) {
                handleError(res, err);
            } else {
                next({
                    'mConference': results.mConference.obj,
                    'commentList': results.commentList.obj
                });
            }
        })
    },
    //添加评论
    addComment: function (req, res, next) {
        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        params.createUid = req.session.uid;
        params.name = req.session.userinfo.name;
        comment.create(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            try {
                if (params.mconference) {
                    stockRecommend.update({rowId: params.mconference}, function () {
                    });
                }
            } catch (e) {
                console.error(e);
            }
            next({
                'comment': result.obj
            });
        });
    },
    //修改评论
    updateComment: function (req, res, next) {
        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        comment.update(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next(result);
        });
    },
    //删除评论
    delComment: function (req, res, next) {
        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        comment.del(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next(result);
        });
    },
    //晨会发言、评论统计
    getMorningconferenceCount: function (req, res, next) {
        var params = req.query;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        params.type = 'morning';
        mConference.getMorningconferenceCount(params, function (err, result) {
            if (err) {
                handleError(res, err);
            } else {
                next(result.obj);
            }
        })
    },
    //创建晨会
    editMConference: function (req, res, next) {
        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }

        var params_stockrecommends = new Array();
        var params_stockrecommend;
        var stock = [];
        if (req.body.company_name_analyst_1 != "" && typeof(req.body.company_name_analyst_1) != "undefined") {
            stock = req.body.company_name_analyst_1.split("_");
            params_stockrecommend = {
                stockId: stock[0],
                stockCode:stock[1],
                stockName:stock[2],
                trade: req.body.operate_analyst_1,
                createUid: req.session.uid
            };
            params_stockrecommends.push(params_stockrecommend);
        }

        if (req.body.company_name_analyst_2 != "" && typeof(req.body.company_name_analyst_2) != "undefined") {
            stock = req.body.company_name_analyst_2.split("_");
            params_stockrecommend = {
                stockId: stock[0],
                stockCode:stock[1],
                stockName:stock[2],
                trade: req.body.operate_analyst_2,
                createUid: req.session.uid
            };
            params_stockrecommends.push(params_stockrecommend);
        }

        if (req.body.company_name_analyst_3 != "" && typeof(req.body.company_name_analyst_3) != "undefined") {
            stock = req.body.company_name_analyst_3.split("_");
            params_stockrecommend = {
                stockId: stock[0],
                stockCode:stock[1],
                stockName:stock[2],
                trade: req.body.operate_analyst_3,
                createUid: req.session.uid
            };
            params_stockrecommends.push(params_stockrecommend);
        }

        if (req.body.type == '公司深度' || req.body.type == '公司点评' || req.body.type == '公司调研') {
            params.analystType = "company";
            params.stockList = params_stockrecommends;
        } else {
            params.analystType = "";
        }

        params.createUid = req.session.uid;
        params.status = '1';
        if (params.page == 'edit') {
            mConference.update(params, function (err, result) {
                if (err) {
                    res.redirect('/error');
                    return;
                } else {
                    next(result.obj);
                }
            });
        } else {
            mConference.create(params, function (err, result) {
                if (err) {
                    res.redirect('/error');
                    return;
                } else {
                    next(result.obj);
                }
            });
        }

    },
    delMConference: function (req, res, next) {
        var params = req.body;
        params.createUid = req.session.uid;
        mConference.delete(params, function (err, result) {
            if (err) {
                next({
                    'code': 404,
                    'message': "删除错误"
                });
            } else {
                next({
                    'code': 200,
                    'message': "删除成功"
                });
            }
        })
    },
    //审批晨会
    approveMConference: function (req, res, next) {

        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        var role = req.session.userinfo.role;
        if (role != '3') {
            next({'errMsg': '没有权限审批晨会'});
            return;
        }
        mConference.update(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            } else {
                next(result.obj);
            }

        });
    },
    //检测是否当前日期已经存在晨会
    checkOne: function (req, res, next) {
        var params = req.query;
        mConference.checkOne(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next(result);
        })
    },
    //按日期获取晨会信息
    getMorningconferenceByDate: function (req, res, next) {
        var params = req.query;
        mConference.getList(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next(result);
        })
    },


    //获取晨会列表
    getMorningconferenceServicetList: function (req, res, next) {
    var params = req.query;
    var role = req.session.userinfo.role;
    var currentDate = new Date();
    var curentDay = currentDate.getDay();
    var currentHour = currentDate.getHours();
    if (curentDay == 6) {
        currentDate.setDate(currentDate.getDate() + 2);
    } else if (curentDay == 0) {

        currentDate.setDate(currentDate.getDate() + 1);
    } else if (currentHour >= 12) {

        currentDate.setDate(currentDate.getDate() + 1);
    }

    var currentDateStr = moment(currentDate).format('YYYY-MM-DD');

    params.date = currentDateStr;


    if (role == '4' || role == '5') {

        params.seller = req.session.userinfo.seller.rowId;

        mConference.getListBySeller(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next({
                    'mconferencelist': result.obj,
                    moment: require("moment")
                }
            );
        })
    } else {
        params.status = '1';
        mConference.getList(params, function (err, result) {
            if (err) {
                res.redirect('/error');
                return;
            }
            next({
                    'mconferencelist': result.obj,
                    moment: require("moment")
                }
            );
        })
    }

}

,

//初始化创建晨会数据
initMorningconference: function (req, res, next) {
    var params = req.query;
    async.series({
        // seller: function (callback) {
        //     //var params = req.query;
        //     //var role = req.session.userinfo.role;
        //     //if (role == '4' || role == '5') {
        //     //    params.rowId = req.session.userinfo.seller.rowId;
        //     //}
        //
        //     seller.getInfo({rowId:req.session.userinfo.seller.rowId}, function (err, result) {
        //         if (err) {
        //             handleError(res, err);
        //         } else {
        //             callback(null, result);
        //         }
        //     })
        // },
        morningconference: function (callback) {
            //var params = req.query;
            //var role = req.session.userinfo.role;
            if (params.page == 'edit' && params.rowId != '') {
                mConference.getInfo({rowId: params.rowId}, function (err, result) {
                    if (err) {
                        handleError(res, err);
                    } else {
                        callback(null, result);
                    }
                })
            } else {
                callback(null, null);
            }
        },
        page: function (callback) {
            callback(null, params.page);
        },
        createUser: function (callback) {
            callback(null, req.session.userinfo);
        },
        industry: function (callback) {
            industry.getIndustryList(null, function (err, result) {
                if (err) {
                    handleError(res, err);
                } else {
                    callback(null, result);
                }
            })
        },
        person: function (callback) {
            var seller = req.session.userinfo.seller.rowId;
            user.getUserListBySeller({seller: seller}, function (err, result) {
                if (err) {
                    handleError(res, err);
                } else {
                    callback(null, result);
                }
            })
        }
    }, function (err, results) {
        if (err) {
            handleError(res, err);
        } else {
            next(results);
        }
    })
}


};

